package bitch;

import java.sql.Timestamp;

import constants.PROTOCOL;

public class ServerParser
	{
	
	private Server server;
	private Database database;
	
	public ServerParser(Server server,Database database)
		{
		this.server = server;
		this.database= database;
		}
	public void parse(String parseString,TCPConnection source)
		{
		String[] pcs = parseString.split("\\|",-1);
		System.out.println(parseString+" - " + pcs.length);
		parseString = '<'+parseString+'>';
		if (pcs.length==0)
			System.out.println("Humbug empty message");
		else 
			{
			Integer type=toInt(pcs[0]);
			if (pcs.length!=PROTOCOL.LEN[type])
				{
				System.out.println("Length mismatch for message type "+type);
				return;
				}
			else
				{
				if(type==PROTOCOL.ROOM)
					{
					int key = toInt(pcs[1]);
					if (key==-1)
						key=database.update("INSERT INTO Room VALUES (DEFAULT,'"+pcs[2]+"',"+pcs[3]+")");
					else
						database.update("UPDATE Room SET roomName='"+pcs[2]+"', size="+pcs[3]+" WHERE roomID="+pcs[1]);
					server.broadcast("<"+PROTOCOL.ROOM+'|'+key+'|'+pcs[2]+'|'+pcs[3]+">");
					System.out.println("Room added or changed, key "+key);
					}
				else if(type==PROTOCOL.DEPARTMENT)
					{
					//Create the Department
					int key = toInt(pcs[1]);
					if (key==-1)
						key=database.update("INSERT INTO Department VALUES (DEFAULT,'"+pcs[2]+"')");
					else
						database.update("UPDATE Department SET departmentName="+pcs[2]+" WHERE departmentID="+pcs[1]);
					server.broadcast("<"+PROTOCOL.DEPARTMENT+'|'+key+'|'+pcs[2]+">");
					System.out.println("Department added or changed");
					}
				else if(type==PROTOCOL.PERSON)
					{
					//Create the Person
					int key = toInt(pcs[1]);
					if (key==-1)
						key=database.update("INSERT INTO Person VALUES (DEFAULT,'"+pcs[2]+"','"+pcs[3]+"','"+pcs[4]+"',"+pcs[5]+",0)");
					else
						database.update("UPDATE Person SET email='"+pcs[2]+"', password='"+pcs[3]+"', realName='"+pcs[4]+"', departmentID="+pcs[5]+" WHERE personID="+pcs[1]);
					server.broadcast("<"+PROTOCOL.PERSON+'|'+key+'|'+pcs[2]+'|'+"-1"+'|'+pcs[4]+'|'+pcs[5]+">");//Password is not broadcasted.
					System.out.println("Person added or changed");
					}
				else if(type==PROTOCOL.APPOINTMENT)
					{
					server.setAppointment(toInt(pcs[1]),toInt(pcs[2]),toTimestamp(pcs[3]),toTimestamp(pcs[4]),pcs[5],pcs[6],toInt(pcs[7]),pcs[8],toInt(pcs[10]),toInt(pcs[11]),parseString,source);
					System.out.println("Appointment added");
					}
				else if(type==PROTOCOL.MESSAGE)
					{
					System.out.println("You don't send messages to the server");
					}
				else if(type==PROTOCOL.DEL_ROOM)
					{
					//database.update("DELETE FROM Room WHERE roomID="+pcs[1]+"");
					//server.broadcast(parseString);
					System.out.println("Illegal operation(DEL_ROOM)");
					}
				else if(type==PROTOCOL.DEL_DEPARTMENT)
					{
					//database.update("DELETE FROM Department WHERE departmentID="+pcs[1]+"");
					//server.broadcast(parseString);
					System.out.println("Illegal operation(DEL_DEPARTMENT)");
					}
				else if(type==PROTOCOL.DEL_PERSON)
					{
					//database.update("DELETE FROM Person WHERE personID="+pcs[1]+"");
					//server.broadcast(parseString);
					System.out.println("Illegal operation(DEL_PERSON)");
					}
				else if(type==PROTOCOL.DEL_APPOINTMENT)
					{
					server.delAppointment(toInt(pcs[1]),toInt(pcs[2]),pcs[3],parseString);
					System.out.println("Appointment cancelled");
					}
				else if(type==PROTOCOL.DEL_MESSAGE)
					{
					database.update("DELETE FROM Message WHERE ownerID="+pcs[1]+" AND messageID="+pcs[2]+"");
					System.out.println("Message deleted");
					}
				else if(type==PROTOCOL.LOGIN_ATTEMPT)
					{
					System.out.println("Login attempt: "+pcs[1]+", "+pcs[2]);
					server.login(pcs[1],pcs[2],source);
					}
				else if(type==PROTOCOL.LOGIN_ACCEPT)
					{
					System.out.println("Server doesn't need to login anywhere");
					}
				else if(type==PROTOCOL.LOGIN_ERROR)
					{
					System.out.println("Server certainly cannot fail to login");
					}
				else if(type==PROTOCOL.SET_ATTENDANCE)
					{
					server.setAttendance(toInt(pcs[3]),toInt(pcs[1]),toInt(pcs[2]),toInt(pcs[4]),pcs[5]);
					System.out.println("Attendance set");
					}
				else if(type==PROTOCOL.ACCESS_RANGE)
					{
					server.accessRange(toTimestamp(pcs[1]),toTimestamp(pcs[2]),source);
					System.out.println("Appointments sent");
					}
				else if(type==PROTOCOL.ACCESS_PERSON)
					{
					server.accessPerson(toInt(pcs[1]),source);
					System.out.println("Appointments sent");
					}
				else if(type==PROTOCOL.ACK_APPOINTMENTS)
					{
					System.out.println("Server doesn't request appointments");
					}
				else if(type==PROTOCOL.GET_APPOINTMENT)
					{
					server.getAppointment(toInt(pcs[1]),toInt(pcs[2]),source);
					System.out.println("Appointment sent");
					}
				else if(type==PROTOCOL.UNSET_PERSON)
					{
					server.accesses.get(source.getPersonID()).unsetPerson(toInt(pcs[1]));
					System.out.println("Person access unset");
					}
				else if(type==PROTOCOL.UNSET_SPECIFIC)
					{
					server.accesses.get(source.getPersonID()).accessSpecific(-1, -1);
					System.out.println("Specific access unset");
					}
				else if(type==PROTOCOL.POKE)
					{
					System.out.println("Poke!");
					}
				else if(type==PROTOCOL.CHECK_ROOM)
					{
					server.checkAvailability(toInt(pcs[1]),toTimestamp(pcs[2]),toTimestamp(pcs[3]),toInt(pcs[4]),toInt(pcs[5]),source);
					System.out.println("Room availability check");
					}
				else
					System.out.println("Server cannot handle that humbug command");
				}
			}
		}
	public Integer toInt(String str)
		{
		try 
			{
			return Integer.parseInt(str);
			}
		catch (NumberFormatException e)
			{
			System.out.println("Humbug number format cannot be parsed");
			return null;
			}
		}
	public Long toLong(String str)
		{
		try 
			{
			return Long.parseLong(str);
			}
		catch (NumberFormatException e)
			{
			System.out.println("Humbug number format cannot be parsed");
			return null;
			}
		}
	public Boolean toBoolean(String str)
		{
		if(str=="1")
			return true;
		else
			return false;
		}
	public Timestamp toTimestamp(String str)
		{
		return new Timestamp(toLong(str));
		}
	}
